/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.entitygen.method;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.designtime.api.element.GspElementObjectType;
import com.inspur.edp.cef.gencommon.ReflectionUtils;
import com.inspur.edp.cef.generator.common.MediatePropertyInfo;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.EntityGeneratorContext;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.property.SimpleUdtDataValuePropGenerator;
import com.inspur.edp.cef.generator.overall.Utils;
import com.inspur.edp.udt.designtime.api.entity.SimpleDataTypeDef;
import com.inspur.edp.udt.designtime.api.entity.UnifiedDataTypeDef;
import com.inspur.edp.udt.entity.ISimpleUdtData;
import java.util.ArrayList;
import java.util.List;
import lombok.var;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CastExpression;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.InstanceofExpression;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.SwitchCase;
import org.eclipse.jdt.core.dom.SwitchStatement;
import org.eclipse.jdt.core.dom.ThrowStatement;
import org.eclipse.jdt.core.dom.Type;

public class SetValueGenerator extends ClassMethodGenerator {

  private static final String MethodName = "innerSetValue";
  private static final String param1 = "propName";
  private static final String param2 = "value";
  private final EntityGeneratorContext ctx;
  private final String nodeCode;

  public SetValueGenerator(EntityGeneratorContext ctx,String nodeCode) {
    this.ctx = ctx;
    this.nodeCode = nodeCode;
  }

  public static Type getType(AST ast, TypeInfo udtItemType) {
    if (udtItemType.getTypeName().equals("byte[]")) {
      return new com.inspur.edp.caf.generator.baseInfo.TypeInfo(byte[].class).getType(ast);
    } else {
      return ast.newSimpleType(ast.newName(udtItemType.getTypeFullName()));
    }

  }

  @Override
  protected String getMethodName() {
    return MethodName;
  }

  @Override
  protected TypeInfo getReturnType() {
    return new TypeInfo(void.class.getTypeName());
  }

  @Override
  protected boolean getIsOverride() {
    return true;
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
    list.add(ModifierKeyword.PROTECTED_KEYWORD);
    return list;
  }

  @Override
  protected ArrayList<ParameterInfo> getParameterCollection() {
    ArrayList<ParameterInfo> list = new ArrayList<>();
    ParameterInfo parameterInfo = new ParameterInfo();
    parameterInfo.setParamType(new TypeInfo(String.class.getTypeName()));
    parameterInfo.setParamName(param1);
    list.add(parameterInfo);

    parameterInfo = new ParameterInfo();
    TypeInfo typeInfo = new TypeInfo(Object.class.getTypeName());
    parameterInfo.setParamType(typeInfo);
    parameterInfo.setParamName(param2);
    list.add(parameterInfo);
    return list;
  }

  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();
    MethodInvocation methodInvocation=ast.newMethodInvocation();
    methodInvocation.setExpression(ast.newSimpleName(ctx.getDataImplTypes().get(nodeCode).getFullName()));
    methodInvocation.setName(ast.newSimpleName(DataStaticSetValueGenerator.methodName));
    methodInvocation.arguments().add(ast.newThisExpression());
    methodInvocation.arguments().add(ast.newSimpleName(param1));
    methodInvocation.arguments().add(ast.newSimpleName(param2));

    block.statements().add(ast.newExpressionStatement(methodInvocation));
    return block;
  }

//  private void setSwitchExpresssion(SwitchStatement switchStatement) {
//    switchStatement.setExpression(ast.newSimpleName(param1));
//  }
//
//  private void setSwitchCases(SwitchStatement switchStatement) {
//    for (MediatePropertyInfo propertyInfo : propertyInfos) {
//      buildPropertyCaseStatement(propertyInfo, switchStatement);
//    }
//  }
//
//  private void buildPropertyCaseStatement(MediatePropertyInfo propertyInfo,
//      SwitchStatement switchStatement) {
////    ChildAttribute childAttribute = (ChildAttribute) ReflectionUtils
////        .getMethodAnnotation(propertyInfo.getPropertyMethod(), ChildAttribute.class);
//    if (propertyInfo.getNode() != null) {
//      return;
//    }
//    if(propertyInfo.getExtendInfos().containsKey("AssoUdtAtrribute"))
//      return;
//
//    SwitchCase switchCase1 = ast.newSwitchCase();
//
//    StringLiteral stringLiteral = ast.newStringLiteral();
//    stringLiteral.setLiteralValue(propertyInfo.getPropertyName());
//    switchCase1.setExpression(stringLiteral);
//    switchStatement.statements().add(switchCase1);
//
//    setSwitchCaseBody(switchStatement, propertyInfo);
//
//    switchStatement.statements().add(ast.newBreakStatement());
//  }
//
//  private void setSwitchCaseBody(SwitchStatement switchStatement,
//      MediatePropertyInfo propertyInfo) {
//    UnifiedDataTypeDef udt = Utils.getUdt(propertyInfo.getElement(),
//        parentContext.getProjContext().getCefGenContext());
//    if (udt != null && udt instanceof SimpleDataTypeDef) {
//      TypeInfo genericType = SimpleUdtDataValuePropGenerator.getSimpleUdtDataGenericType((SimpleDataTypeDef) udt);
//
//      setUdtSwitchCaseBody(switchStatement, propertyInfo, (SimpleDataTypeDef) udt, genericType);
//    } else {
//      setSimpleSwitchCaseBody(switchStatement, propertyInfo, propertyInfo);
//    }
//  }
//
//  private void setSimpleSwitchCaseBody(SwitchStatement switchStatement,
//      MediatePropertyInfo propertyInfo, MediatePropertyInfo propertyType) {
//    MethodInvocation methodInvocation = ast.newMethodInvocation();
//    methodInvocation.setName(ast.newSimpleName(propertyInfo.getSetterMethodName()));
//
//    CastExpression castExpression = ast.newCastExpression();
//    castExpression
//        .setType(propertyType.getPropertyType().getType(ast, parentContext.getProjContext()));
//
//    if (propertyType.getElement().getObjectType() == GspElementObjectType.Enum && !propertyType
//        .getElement().getIsUdt()) {
//      addImport("com.inspur.edp.cef.spi.entity.ValueConvertUtils");
//      var enumValueInvocation = ExpressionUtils
//          .getMethodInvocation(ast, "ValueConvertUtils", "toEnum");
//
//      var typeLiteral = ast.newTypeLiteral();
//      typeLiteral
//          .setType(propertyType.getPropertyType().getType(ast, parentContext.getProjContext()));
//      enumValueInvocation.arguments().add(typeLiteral);
//      enumValueInvocation.arguments().add(ast.newSimpleName(param2));
//
//      castExpression.setExpression(enumValueInvocation);
//
//    } else {
//      castExpression.setExpression(ast.newSimpleName(param2));
//    }
//
//    methodInvocation.arguments().add(castExpression);
//    ExpressionStatement expressionStatementx = ast.newExpressionStatement(methodInvocation);
//    switchStatement.statements().add(expressionStatementx);
//  }
//
//  private void setUdtSwitchCaseBody(SwitchStatement switchStatement,
//      MediatePropertyInfo propertyInfo, SimpleDataTypeDef udt, TypeInfo genericType) {
//    IfStatement ifStatement = ast.newIfStatement();
//    setUdtSwitchCaseIfExpression(ifStatement, propertyInfo, genericType);
//    setUdtSwitchCaseIfThen(ifStatement, propertyInfo, genericType);
//    setUdtSwitchCaseIfElse(ifStatement, propertyInfo, genericType);
//
//    switchStatement.statements().add(ifStatement);
//  }
//
//  private void setUdtSwitchCaseIfExpression(IfStatement ifStatement,
//      MediatePropertyInfo propertyInfo, TypeInfo genericType) {
//    InfixExpression infixExpression1 = ast.newInfixExpression();
//    infixExpression1.setOperator(InfixExpression.Operator.CONDITIONAL_AND);
//    InfixExpression infixExpression = ast.newInfixExpression();
//    infixExpression.setOperator(InfixExpression.Operator.NOT_EQUALS);
//
//    MethodInvocation methodInvocation = ast.newMethodInvocation();
//    methodInvocation.setName(ast.newSimpleName(propertyInfo.getGetterMethodName()));
//    infixExpression.setLeftOperand(methodInvocation);
//    infixExpression.setRightOperand(ast.newNullLiteral());
//    infixExpression1.setLeftOperand(infixExpression);
//
//    InstanceofExpression instanceofExpression = ast.newInstanceofExpression();
//    instanceofExpression.setLeftOperand(ast.newSimpleName(param2));
//    instanceofExpression.setRightOperand(genericType.getType(ast));
//    addImport(genericType);
//    //instanceofExpression.setRightOperand(ast.newSimpleType(ast.newName(udtItemType.getTypeName())));
//    infixExpression1.setRightOperand(instanceofExpression);
//
//    ifStatement.setExpression(infixExpression1);
//  }
//
//  private void setUdtSwitchCaseIfThen(IfStatement ifStatement, MediatePropertyInfo propertyInfo,
//      TypeInfo genericType) {
//    Block block = ast.newBlock();
//    MethodInvocation methodInvocation = ast.newMethodInvocation();
//
//    MethodInvocation methodInvocation1 = ast.newMethodInvocation();
//    methodInvocation1.setName(ast.newSimpleName(propertyInfo.getGetterMethodName()));
//    methodInvocation.setExpression(methodInvocation1);
//
//    methodInvocation.setName(ast.newSimpleName("setValue"));
//
//    CastExpression castExpression = ast.newCastExpression();
//    Class udtItemType = null;
//
//    castExpression.setType(genericType.getType(ast));
//    //castExpression.setType(ast.newSimpleType(ast.newName(udtItemType.getTypeName())));
//    castExpression.setExpression(ast.newSimpleName(param2));
//    methodInvocation.arguments().add(castExpression);
//
//    ExpressionStatement expressionStatement = ast.newExpressionStatement(methodInvocation);
//    block.statements().add(expressionStatement);
//    ifStatement.setThenStatement(block);
//  }
//
//  private void setUdtSwitchCaseIfElse(IfStatement ifStatement, MediatePropertyInfo propertyInfo,
//      TypeInfo genericType) {
//    Block block = ast.newBlock();
//    MethodInvocation methodInvocation = ast.newMethodInvocation();
//    methodInvocation.setName(ast.newSimpleName(propertyInfo.getSetterMethodName()));
//
//    CastExpression castExpression = ast.newCastExpression();
//    castExpression.setType(propertyInfo.getPropertyType().getType(ast));
//    //castExpression.setType(ast.newSimpleType(ast.newName(propertyType.getTypeName())));
//    //castExpression.setType(new com.inspur.edp.caf.generator.baseInfo.TypeInfo(byte[].class).getType(ast));
//    castExpression.setExpression(ast.newSimpleName(param2));
//    methodInvocation.arguments().add(castExpression);
//
//    ExpressionStatement expressionStatement = ast.newExpressionStatement(methodInvocation);
//    block.statements().add(expressionStatement);
//    ifStatement.setElseStatement(block);
//  }
//
//  private void setSwitchDefaultCase(SwitchStatement switchStatement) {
//    SwitchCase defaultCase = ast.newSwitchCase();
//    defaultCase.setExpression(null);
//    switchStatement.statements().add(defaultCase);
//
//    ThrowStatement throwStatement = ast.newThrowStatement();
//    ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
//
//    classInstanceCreation.setType(
//        new com.inspur.edp.caf.generator.baseInfo.TypeInfo(RuntimeException.class).getType(ast));
//
//    InfixExpression infixExpression = ast.newInfixExpression();
//    infixExpression.setOperator(InfixExpression.Operator.PLUS);
//    StringLiteral stringLiteral = ast.newStringLiteral();
//    stringLiteral.setLiteralValue("属性名不存在：");
//    infixExpression.setLeftOperand(stringLiteral);
//    infixExpression.setRightOperand(ast.newSimpleName(param1));
//    classInstanceCreation.arguments().add(infixExpression);
//
//    throwStatement.setExpression(classInstanceCreation);
//    switchStatement.statements().add(throwStatement);
//  }

}
